package com.bl.weilan.manager.controller.report;

import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.dao.model.ReportEntity;
import com.bl.weilan.manager.controller.BaseController;
import com.bl.weilan.service.biz.report.ReportService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.report.ReportAddVo;
import com.bl.weilan.service.vo.report.ReportListVo;
import com.bl.weilan.service.vo.report.ReportModVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by liangweikang on 18/12/26.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/weilan/manager/report"})
@Api(value = "报告相关", description = "报告相关api")
public class ReportController extends BaseController {
    @Autowired
    ReportService reportService;


    @ApiOperation(value = "获取报告列表,", notes = "获取报告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_report_list"})
    public ApiResponseBody<ReportListVo> reportList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        ReportListVo reportListVo = reportService.getReportLists(pageable);

        return ApiResponseBody.createSuccessBody(reportListVo);
    }

    @ApiOperation(value = "获取一条报告详情信息", notes = "获取一条报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_report_byid"})
    public ApiResponseBody<ReportListVo> reportList(
            @RequestParam(value = "id", required = true) String id,
            HttpServletRequest request) {

        ReportEntity reportEntity = reportService.getReportById(id);

        return ApiResponseBody.createSuccessBody(reportEntity);
    }

    @ApiOperation(value = "增加报告", notes = "增加报告信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_report"})
    public ApiResponseBody add(
            @Valid @RequestBody ReportAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        reportService.addReport(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "修改报告信息", notes = "修改报告信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify"})
    public ApiResponseBody modify(
            @Valid @RequestBody ReportModVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        reportService.modify(body);

        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "删除批量", notes = "删除批量报告 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_list"})
    public ApiResponseBody delList(
            @Valid @RequestBody DelListBody body, BindingResult result,
            HttpServletRequest request) {

        reportService.delList(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除单个报告信息", notes = "删除单个报告信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {


        reportService.del(body);

        return ApiResponseBody.createSuccessBody(null);
    }



}
